/******************************************************************************/
/*! main.cpp
    [2012]
    \file		main.cpp
    \brief      entry point of the engine
*/
/******************************************************************************/
/*****************************************************************/ 
//Includes 
/*****************************************************************/
#include "NeroGame\Nero.h"

#include "GraphicsApp.h"
#include "TriangleApp.h"
#include "CubeApp.h"

/*** Globals ******************************************************************/
extern Engine::NPlatform*	GPlatform;

/*** Defines ******************************************************************/
#define USE_RENDER_THREAD				1
#define MEMORY_MANAGERMENT_ENABLED		1
#define LUA								0

/*** Scene To Load ************************************************************/
std::string GScene = "..\\..\\assets\\xml\\3D_Game_World.xml";

/*** Platform Includes ********************************************************/
#if LUA
#pragma comment(lib, "lua51.lib")
#endif

#ifdef WIN32
#ifdef _DEBUG
#pragma comment(lib, "Win32_D.lib")
#else
#pragma comment(lib, "Win32.lib")
#endif

#include "Win32\Win32.h"

/*** Rendering Library ********************************************************/
#include "DirectX\Device.h"

#ifdef _DEBUG
#pragma comment(lib, "DirectX_D.lib")
#else
#pragma comment(lib, "DirectX.lib")
#endif

/*** User Library *************************************************************/
#pragma comment(lib,"user32.lib") 

/*** Initialize Globals *******************************************************/
void GInitialize(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int showCommand)
{
#ifdef MEMORY_MANAGERMENT_ENABLED
	Memory::MemoryManager::Initialize();
#endif

	GPlatform = MY_NEW(Memory::HID_Engine,"Platform Win32") System::Win32Platform(instance, previousInstance, commandLine, showCommand);
	
	Game::NGameEngine::Initialize(MY_NEW(Memory::HID_Engine,"Application") GraphicsApp());
}

/*** Deinitialize Globals ****************************************************/
void GDeinitialize()
{
	Game::NGameEngine::Deinitialize();
	
	MY_DELETE(GPlatform);

#if MEMORY_MANAGERMENT_ENABLED
	DUMP_MEMORY
	Memory::MemoryManager::Deinitialize();
#endif
}

#if LUA
/*** LUA *********************************************************************/
extern "C"
{
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

typedef int (*lua_CFunction) (lua_State *L);

static int average(lua_State *L)
{
	/* get number of arguments */
	int n = lua_gettop(L);
	double sum = 0;
	int i;

	/* loop through each argument */
	for (i = 1; i <= n; i++)
	{
		/* total the arguments */
		sum += lua_tonumber(L, i);
	}

	/* push the average */
	lua_pushnumber(L, sum / n);

	/* push the sum */
	lua_pushnumber(L, sum);

	/* return the number of results */
	return 2;
}

int main ( int argc, char *argv[] )
{
	lua_State* L = lua_open();
	luaL_openlibs(L);

	lua_register(L, "average", average);

	int error;
	error = luaL_dofile(L, "..\\..\\scripts\\avg.lua");
	if(error)
	{
		std::string error = lua_tostring(L, -1);
		lua_pop(L, 1);  /* pop error message from the stack */
	}
	error = luaL_dofile(L, "..\\..\\scripts\\HelloWorld.lua");
	if(error)
	{
		std::string error = lua_tostring(L, -1);
		lua_pop(L, 1);  /* pop error message from the stack */
	}
	lua_close(L);
	printf( "Press enter to exit..." );
	getchar();
}
#else
/***  Program Entry Point ****************************************************/
/*int WINAPI WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int showCommand)
{
	GInitialize(instance, previousInstance, commandLine, showCommand);

#if USE_RENDER_THREAD
	StartRenderThread();
#endif

	Game::NGameEngine::Run();

	GDeinitialize();
}*/
#endif
#elif defined(XBONE)
#elif defined(PS4)
#endif